import chatModel from "@/ChatMocel";
import { getUserInfo } from "@/Util";
import { ChatMsgInfo, InviteMsgType } from "@/Util/Constant";
import eventBus from "@/Util/EventBus";
import Hlddz from "@/game/model/hlddz";
import { DoubleMsgType, SummaryHappyBean } from "@/share/commonType";
import socket from "@/socket/UserChatSocket";
import { ElMessage } from "element-plus";

export function UserStatusChangeHandler(userlist: string[]) {
    let friends = socket.friendList
    let flag = false
    for (let i = 0; i < friends.length; i++) {
        if (userlist.includes(friends[i])) { // 好友里边有人状态变化了
            flag = true
            break;
        }
    }
    if (flag) {
        eventBus.emit('updateFriendList')
    }
}
export function chatMessageHandler(msg: ChatMsgInfo) {
    let { receiver, sender } = msg
    let myname = chatModel.username
    if (sender === myname) return; //这条消息是你本人发的
    if (receiver !== myname) return; // 这条消息不是发给你的
    // 这条消息是发给你的
    chatModel.addChatMsg(msg)
    eventBus.emit('updateScroller', true)
}

export function InviteFriendHandler(msg: any) {
    let { gameid, inviteUser, sender, nickname, avatar, gameName } = msg
    let myname = chatModel.username
    if (inviteUser !== myname) return; //这条邀请信息并不是发给你的
    // 这条信息是发给你的
    let inviteMsg: InviteMsgType = { sender, gameid, nickname, avatar, gameName }
    eventBus.emit('showGameInviteBar', inviteMsg) // 显示邀请条
}

export function ChangeStatusHandler(msg: any) {
    let { gameId, players } = msg;
    let model = Hlddz.getInstance();
    if (gameId !== model?.gameId) return;
    model.changePlayer(players)
}

export function AcceptInviteHandler(msg: any) { // 收到一条接受邀请的消息
    // sender 表示这条消息的原生邀请者
    // inviteUser 表示接受邀请的好友
    let { gameid, inviteUser, sender, players } = msg;
    let myname = chatModel.username
    if (myname === inviteUser) return; //这条邀请是邀请你的，你自己不在这里处理
    if (sender === myname) {
        // 这邀请信息是你发出去的
        // 在这里可以做一些通知
    };
    let model = Hlddz.getInstance();
    if (gameid !== model?.gameId) {
        return ElMessage({
            message: '该玩家已退出游戏',
            type: 'warning',
        })
    }
    model.changePlayer(players)
}

export function OutCardHandler(msg: any) {
    let model = Hlddz.getInstance();
    if (msg.gameid !== model?.gameId) return;
    model.setLastOutCardInfo(msg)
}

export function DoubleHandler(msg: DoubleMsgType) {
    let model = Hlddz.getInstance();
    if (msg.gameid !== model?.gameId) return;
    model.changeMultiple(msg)
}

export function QuitGameHandler(msg: any) {
    // sender 表示这条消息的原生邀请者
    // inviteUser 表示接受邀请的好友
    let { gameid, username, players } = msg;
    let myname = getUserInfo().username;
    // 这条信息是你发的
    if (myname === username) return;
    let model = Hlddz.getInstance();
    if (gameid !== model?.gameId) return;
    model.changePlayer(players);
}

export function GameOverHandler(msg: any) {
    let { gameid, summaryObj } = msg;
    let model = Hlddz.getInstance();
    if (gameid !== model?.gameId) return;
    model.gameOver(summaryObj)
} 